/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marco
 */
public class TCPService implements Connection {

    private int port;
    private Service servant;
    private ServerSocket ss = null;
    private Socket socket = null;
    private BufferedReader br;

    public TCPService(Service servant, int port) {
        this.servant = servant;
        this.port = port;
    }

    @Override
    public void run() {
        try {
            while (true) {
                ss = new ServerSocket(port);
                socket = ss.accept();
                System.out.println("Waiting connections");
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String event = br.readLine();
                servant.enqueue(event);
                br.close();
                socket.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(TCPService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
